<?php 

/**
 * 第三方账号连接
 * @author keluo<pycorvn@yeah.net>
 * @since 2012-12-9 15:38:34
 *
 */
class ConnectController extends SCController {

	public function __construct() {
		parent::__construct();
	}
	
	public function index() {
		header('Content-Type:text/html;charset=utf-8');
		$text = '@微观世界$\@@333、@微@观x|".世界 @abcee* @abcd@xx|@c;@尺寸； 国威大厦@pycorvn{@a/b}@xxxx&';
		$pattern = '/@([^ @\|\$\\\.\'\(\)\[\]\{\}\/\*\-\+#";；、&]+)/i';
		preg_match_all($pattern, $text, $matches);
		$users = array_unique($matches[1]);
		echo '<pre>';
		#print_r($matches);
		print_r($users);
		die;
		
	}
	/**
	 * 授权认证
	 * @param string $vendor 第三方平台商
	 */
	public function authorize($vendor) {
		$this->factory($vendor)->authorize();
	}
	
	/**
	 * 授权认证回调
	 * @param string $vendor 第三方平台商
	 */
	public function authBack($vendor) {
		$openId = $this->factory($vendor)->authBack();
		
		if($this->factory($vendor)->isBind($openId)) { // 已绑定为本网站用户
			$user_id = $this->factory($vendor)->getBindUserId($openId);
			SCScabish::model('user')->setLogin($user_id);
			SCRedirect::to('home');
		} else {
			SCRedirect::to('connect/bind/vendor/'.$vendor);
		}
		
	}

	/**
	 * 将第三方平台用户绑定到本网站账号
	 */
	public function bind($vendor) {
		require $this->view->load();
	}
	
	public function doBind($vendor) {
		$this->factory($vendor)->bind();
	}
	
	/**
	 * Access Token回调
	 * @param string $vendor 第三方平台商
	 */
	public function tokenBack($vendor) {
		file_put_contents('a.txt', implode('|', $_REQUEST).PHP_EOL.file_get_contents('a.txt'));
		#$access_token = $this->factory($vendor)->tokenBack();
		#echo $access_token;
	}
	
	protected function factory($vendor) {
		static $vendors = array();
		if(!array_key_exists($vendor, $vendors)) {
			switch($vendor) {
				case 'tencent':
					$vendors[$vendor] = new Tencent;
					break;
			
				case 'sina':
					$vendors[$vendor] = new Sina;
					break;
			}
		}
		return $vendors[$vendor];
		
		
	}
	
}